//
//  MCMarketingListViewController.swift
//  StoreManage
//
//  Created by MC on 2018/6/25.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCMarketingListViewControllerClosure = (UIViewController) -> ()

class MCMarketingListViewController: MCViewController {
    
    public var closure : MCMarketingListViewControllerClosure?
    
    public var activityType = 0 // 类型(0.全部 2.福利社 3体验4拼团5秒杀6预售7促销活动8社区分销)
    
    private var pageNumber = 1
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
        
        sendNetworking()
    }
    
    // MARK: - System Delegate
    deinit {
        MCNotification_remove(vc: self, name: NOTIFICATION_ChangeStore)
        MCNotification_remove(vc: self, name: NOTIFICATION_addSocialDistribution)
    }

    // MARK: - Setter & Getter
    lazy private var tableView: MCTableView = {
        let tableView = MCTableView.init(frame: CGRect.zero, style: UITableViewStyle.plain)
        tableView.delegate = self
        tableView.dataSource = self
        return tableView
    }()
    
    lazy var dataArrayM: NSMutableArray = {
        let arrayM = NSMutableArray()
        return arrayM
    }()
    

}

extension MCMarketingListViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.dataArrayM.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 276
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = MCMarketingListCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        cell.closure = { activityId  in
           
            let vc = MCMarketingDetailViewController()
            vc.activityId = activityId
            self.closure?(vc)
        }
        
        cell.model = self.dataArrayM[indexPath.row] as! MCMarketingListModel
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataArrayM[indexPath.row] as! MCMarketingListModel
        
        let vc = MCMarketingDetailViewController()
        vc.activityId = model.activityId.intValue
        closure?(vc)
    }
}


//MARK: 通知回调，闭包回调，点击事件
extension MCMarketingListViewController {
    @objc func reloadData() {
        self.dataArrayM.removeAllObjects()
        tableView.reloadData()
        pageNumber = 1
        sendNetworking()
    }
}


//MARK: 网络请求
extension MCMarketingListViewController {
    func sendNetworking() {
        
        let para = [
            "activityType" : activityType,
            "pageNum" : pageNumber
            ] as [String : Any]
        
        MCNetwork.POST(JAVA_findActivityInfoListByPage, para, vc: self, success: { (data) in
    
            let dataArray = data.arrayObject ?? []
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            if self.pageNumber == 1 {
                self.dataArrayM.removeAllObjects()
                self.tableView.mj_footer.resetNoMoreData()
            }
            
            if dataArray.count == 0 {
                self.tableView.mj_footer.endRefreshingWithNoMoreData()
            }
            
            if dataArray.count + self.dataArrayM.count == 0 {
                self.tableView.showBgImage()
            } else {
                self.tableView.hideBgImage()
                for dict in dataArray {
                    let model = MCMarketingListModel()
                    model.setValuesForKeys(dict as? [String:Any] ?? [:])
                    self.dataArrayM.add(model)
                }
                
                self.tableView.reloadData()
            }

            
        }, failure: nil)
    }

}

//MARK: UI的处理,通知的接收
extension MCMarketingListViewController {
    func baseSetting() {
        self.tableView.mj_header = MCRefreshHeader.init(refreshingBlock: {
            self.pageNumber = 1
            self.sendNetworking()
        })
        
        self.tableView.mj_footer = MCRefreshFooter.init(refreshingBlock: {
            self.pageNumber += 1
            self.sendNetworking()
        })
        
        MCNotification_addObserver(name: NOTIFICATION_ChangeStore, vc: self, selector: #selector(reloadData))
        MCNotification_addObserver(name: NOTIFICATION_addSocialDistribution, vc: self, selector: #selector(reloadData))
        
    }
    
    func initUI() {
        view.addSubview(tableView)
        tableView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(view)
        }
    }
}


